﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using Portal.Systems.Model;
using Portal.Web.Utility;
using Portal.Web.Model;
using Portal.Utility;

namespace Portal.Systems.Render
{
    public class WorkflowDefRenderBll : WebPartTemplate
    {
        public override string Id
        {
            get
            {
                return "WorkflowDefRenderBll";
            }
        }

        public override string Title
        {
            get
            {
                return "Quản lý quy trình trên cổng";
            }
        }



        static void CheckPermission(string SiteId)
        {
            try
            {
                string UserId = WebSessionUtility.GetCurrentLoginUser().UserId;
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, SystemPermission.StaticId, SystemPermission.funcManage, SystemPermission.funcManage, UserId, true);
            }
            catch (Exception ex)
            {
                string ReturnData = "<div class=CssAccessDenied>" + ex.Message.ToString() + " <span class=CssBack><a href=\"javascript:history.back(-1);\">Quay lại</a></span></div>\r\n";
                throw new Exception(ReturnData);
            }
        }
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CheckPermission(SiteId);
                string UserType = (string)WebEnvironments.Request(SiteId, "type");
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                if (string.IsNullOrEmpty(UserType)) UserType = "";
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string Html =
                   "<script language=javascript>\r\n" +
                   "    function ClientReading()\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        AjaxOut = Portal.Systems.Render.WorkflowDefRenderBll.ServerSideReading(SiteId).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divListWorkflowDef').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n" +


                   "</script>\r\n" +
                   "<div id=divActionForm style=\"display:none;\"></div>\r\n" +
                   "<input type=hidden id=txtUserType value=\"" + UserType + "\">\r\n" +
                   "<div class=FormFunction id=divListForm>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:window.open('../../folder.aspx?folder=admin','_self');\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "  <div id=\"divListWorkflowDef\" class=tabcontent>\r\n" +
                         ServerSideReading(SiteId).HtmlContent +
                   "  </div>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = "<div class=CssAccessDenied>" + ex.Message.ToString() + "</div>\r\n";
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReading(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {

                WorkflowDefCls[] WorkflowDefs = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingWorkflowDef(SiteId);
                string Subject = "Danh sách quy trình trên cổng";
                string Html =
                   "        <h3>" + Subject + "</h3>\r\n" +
                   "            <table>\r\n" +
                   "                <tr>\r\n" +
                   "                    <th class=thIndex>#</th>\r\n" +
                   "                    <th class=thCode>Quy trình</th>\r\n" +
                   "                    <th></th>\r\n" +
                   "                </tr>\r\n";
                for (int iIndex = 0; iIndex < WorkflowDefs.Length; iIndex++)
                {
                    string ConfigButton = "<input type=button class=button value=\"Thiết lập\" onclick=\"javascript:window.open('WorkflowManRoleConfig.aspx?WorkflowDefId=" + WorkflowDefs[iIndex].WorkflowDefId + "','_self');\">";
                    Html +=
                       "    <tr>\r\n" +
                       "        <td class=tdIndex>" + (iIndex + 1) + "</td>\r\n" +
                       "        <td class=tdCode>" + WorkflowDefs[iIndex].WorkFlowName + "</td>\r\n" +
                       "        <td style=\"width:10px;\">" + ConfigButton + "</td>\r\n" +
                       "   </tr>\r\n";
                }
                Html +=
                   "    </table>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }
    }
}
